//
//  AMMessageDataModule.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOMessageDataModule.h"
#import "HOMessageEntity.h"
#import "HOConstants.h"
#import "HODefine.h"
#import "HOUIMgr.h"
#import "Logger.h"
#import "HOUtils.h"



@implementation HOMessageDataModule
@synthesize     orderMessageList;
@synthesize		systemMessageList;
@synthesize		lastMessageList;
- (void)initData
{
    messageDic = [[NSMutableDictionary alloc]init];
    self.orderMessageList = [[[NSMutableArray alloc]init] autorelease];
	self.systemMessageList = [[[NSMutableArray alloc]init] autorelease];
	self.lastMessageList = [[[NSMutableArray alloc]init] autorelease];
	
    lock = [[NSObject alloc] init];
    newMessageDic = [[NSMutableDictionary alloc]init];
    [self initDefaultMessage];
}

- (void)initDefaultMessage
{
}

- (BOOL)hasUnreadMsg
{
//    for(HOMessageEntity *msg in self.lastMessageList)
//    {
//        if(msg.isUnread)
//        {
//            return YES;
//        }
//    }
    return NO;
}
- (HOMessageEntity *)getMessageWithID:(int)Id
{
    NSString *key = [NSString stringWithFormat:@"%d", Id];
    HOMessageEntity *message = [messageDic objectForKey:key];
    if(message == nil)
    {
		
		message = [[HOMessageEntity alloc]init];
		[messageDic setObject:message forKey:key];
		[message release];
    }
    message = [messageDic objectForKey:key];
    message.messageId = Id;
    return message;
}

- (void)updateNewMessage:(HOMessageEntity *)newMessage
{
//    LogInf(@"update newMessage:%d", newMessage);
//    @synchronized(lock)
//    {
//        HO_MESSAGE_TYPE type = [newMessage getOriType];
//        HOMessageEntity *oldMessage = [newMessageDic objectForKey:[NSNumber numberWithInt:type]];
//        if(oldMessage == nil)
//        {
////            if(type != HO_MESSAGE_TYPE_ADVANCE_QA)
////            {
////                
////                [lastMessageList removeObject:oldMessage];
////                [newMessageDic setObject:newMessage forKey:[NSNumber numberWithInt:type]];
////                [lastMessageList addObject:newMessage];
////            }
////            else
////            {
////                oldMessage.introduction = newMessage.introduction;
////            }
//            
//            [lastMessageList removeObject:oldMessage];
//            [newMessageDic setObject:newMessage forKey:[NSNumber numberWithInt:type]];
//            [lastMessageList addObject:newMessage];
//        }
//        else
//        {
//            if(oldMessage.time < newMessage.time)
//            {
//                
//                [lastMessageList removeObject:oldMessage];
//                [newMessageDic setObject:newMessage forKey:[NSNumber numberWithInt:type]];
//                [lastMessageList addObject:newMessage];
//                //                if(type != HO_MESSAGE_TYPE_ADVANCE_QA)
////                {
////                    [lastMessageList removeObject:oldMessage];
////                    [newMessageDic setObject:newMessage forKey:[NSNumber numberWithInt:type]];
////                    [lastMessageList addObject:newMessage];
////                }
////                else
////                {
////                    oldMessage.time = newMessage.time;
////                    oldMessage.Id = newMessage.Id;
////                    oldMessage.title = newMessage.title;
////                    oldMessage.introduction = newMessage.introduction;
////                }
//            }
//        }
//        LogInf(@"newMessageList:%@", lastMessageList);
//        LogInf(@"newMessageDic:%@", newMessageDic);
//    }
}
#pragma mark

- (void)didReceiveResponse:(HOBaseResponseEntity *)response
{
	if([response isRequestDone])
	{
		if([response isEqualAction:kHO_ACTION_GetMyMessages])
		{
			NSMutableArray *array = response.dataNode;
			for(int i = 0; i < [array count];i ++)
			{
				NSDictionary *dic = [array objectAtIndex:i];
				int messageId = [[dic objectForKey:kHO_FIELD_MessageId] intValue];
				HOMessageEntity *message = [self getMessageWithID:messageId];
				[message parseDic:dic];
				if(![lastMessageList containsObject:message])
					[lastMessageList addObject:message];
			}
		}

	}
	
}

- (HOMessageEntity *)parseMessage:(NSDictionary *)dic
{
	if([[dic allKeys] count] == 0)
		return nil;
	int messageId = [[dic objectForKey:kHO_FIELD_Id] intValue];
	HOMessageEntity *message = [messageDic objectForKey:[NSNumber numberWithInt:messageId]];
	if(message == nil)
	{
		message = [[[HOMessageEntity alloc]init] autorelease];
		[messageDic setObject:message forKey:[NSNumber numberWithInt:messageId]];
	}
	
//	message.Id = messageId;
//	message.messageType = [[dic objectForKey:kHO_FIELD_Type] intValue];
//	message.detailText = [dic objectForKey:kHO_FIELD_Title];
////	message.detailText = @"查看翻译师查看翻译师查看翻译师查看翻译师查看翻译师查看翻译师查看翻译师查看翻译师查看翻译师查看翻译师查看翻译师查看翻译师查看翻译师";
//	message.time = [[dic objectForKey:kHO_FIELD_Time] doubleValue];
//	message.title = [dic objectForKey:kHO_FIELD_Title];
//	message.unreadCount = [[dic objectForKey:kHO_FIELD_NewMessage] intValue];
	return message;
}

- (void)removeMessageFromMemory:(HOMessageEntity *)message
{
//    @synchronized(lock)
//    {
//        [self.lastMessageList removeObject:message];
//        if(message.messageType == HO_MESSAGE_TYPE_ADVANCE_ANNOUNCEMENT)
//        {
//            //公告消息
//            [self.announceMessageList removeObject:message];
//        }
//        else if(message.messageType == HO_MESSAGE_TYPE_ADVANCE_DOMAIN)
//        {
//            [self.domainNewsMessageList removeObject:message];
//        }
//        else if(message.messageType == HO_MESSAGE_TYPE_ADVANCE_SYSTEM)
//        {
//            [self.systemMessageList removeObject:message];
//        }
//        else if(message.messageType == HO_MESSAGE_TYPE_ADVANCE_QA)
//        {
//            [self.QAMessageList removeObject:message];
//        }
//    }
}

- (void)addMessageIntoMemory:(HOMessageEntity *)messageEntity
{
}

#pragma mark - -- DBHandle

//----- message-----

- (void)saveMessageIntoDB:(NSArray *)array
{
//    LogInf(@"saveMessageIntoDB:array:%@", array);
    for(int i = 0; i < [array count]; i ++)
    {
//        HOMessageEntity *message = [array objectAtIndex:i];
    }
}



- (void)updateNewMessageIntoDB:(HOMessageEntity *)newMessage
{
}


- (void)readMessageFromDBWithType:(int)type withMaxId:(int)maxID
{

}

- (void)readNewMessageFromDB
{

}
- (void)deleteNewMessageFromDB:(HOMessageEntity *)message
{

}

- (void)deleteMessageFromDB:(int)type
{

}

- (void)didReadMessageFromDB:(NSArray *)array
{
    return;
}
#pragma mark

- (void)cleanup
{
    [messageDic removeAllObjects];
    [self.orderMessageList removeAllObjects];
    [self.systemMessageList removeAllObjects];
    [newMessageDic removeAllObjects];
	[self.lastMessageList removeAllObjects];
    [self initDefaultMessage];
    [super cleanup];
}

- (void)dealloc
{
    [lock release];
    [messageDic release];
    messageDic = nil;
    
    [newMessageDic release];
    newMessageDic = nil;
    
    
    [self.systemMessageList removeAllObjects];
    self.systemMessageList = nil;
    
    [self.orderMessageList removeAllObjects];
	self.orderMessageList = nil;
	
	[self.lastMessageList removeAllObjects];
	self.lastMessageList = nil;
	
    [super dealloc];
}
@end
